import { requestToData, requestToExport } from '@/api/commonHttp';
import { generateUuid, createExcelByExport } from '@/utils/bizUtils';
import { getBtnsId } from '@/libs/btnsPermission';
// 导出供货信息
const exportInfoData = async (params, enableLoading, stopLoading) => {
  const uuid = generateUuid();
  const res = await requestToExport({
    url: '/sysback/supplyskurel/downLoadTemplateForCreate',
    type: 'post',
    data: {
      manySupply: '1', // 区分是否是多供
      uuid,
      ...params
    }
  }, enableLoading, stopLoading);
  createExcelByExport({ res, excelTitle: `新增多供货信息模板_${uuid}` });
}

// 导出供价信息
const exportPurchaseData = async (params, enableLoading, stopLoading, excelTitle) => {
  const uuid = generateUuid();
  const res = await requestToExport({
    url: '/sysback/supplyareaprice/downLoadTemplateForCreate',
    type: 'post',
    data: {
      manySupply: '1', // 区分是否是多供
      uuid,
      ...params
    }
  }, enableLoading, stopLoading);
  createExcelByExport({ res, excelTitle: `${excelTitle}_${uuid}` });
}

// 导出表单查看页面供价信息
const exportPurchaseDataByView = async (recordNo, enableLoading, stopLoading, excelTitle) => {
  const uuid = generateUuid();
  const res = await requestToExport({
    url: `/sysback/supplyareaprice/downLoadTemplateForSupplyMorePriceRecord/${recordNo}`,
    type: 'post',
  }, enableLoading, stopLoading);
  createExcelByExport({ res, excelTitle: `${excelTitle}_${uuid}` });
}

// 高级关联查询
const getResultsBySeniorSearch = async (data) => {
  const retData = await requestToData({
    url: '/sysback/multiSupplyInfo/seniorGetSkuInfo',
    type: 'post',
    data,
    target: '.body_wrap'
  });
  return retData;
}

// 提交多供数据
const submitMoreSupplyData = async (data, enableLoading, stopLoading) => {
  const retData = await requestToData({
    url: '/sysback/multiSupplyInfo/commitMultiSupplyInfo',
    type: 'post',
    data
  }, enableLoading, stopLoading);
  return retData;
}

// 获取单据信息
const getRecordInfo = async (recordNo) => {
  const retData = await requestToData({
    url: `/sysback/supplyarea/getCreateDetailByRecordNo?recordNo=${recordNo}`,
    type: 'post',
    target: '.body_wrap'
  });
  return retData;
}

// 获取供应商信息
const getSupplyInfo = async (supplyName) => {
  sessionStorage.setItem('buttonId', getBtnsId('关联供应商'));
  const retData = await requestToData({
    url: '/sysback/supplyinfofromsrcm/getSupplyPageFromScrm',
    type: 'post',
    data: {
      supplyName,
      nowPage: 1,
      totalNum: 0,
      pageShow: 10
    },
    isFormData: true,
    target: '.supply_info'
  });
  return retData?.results || [];
}

export default {
  exportInfoData,
  exportPurchaseData,
  submitMoreSupplyData,
  getResultsBySeniorSearch,
  getRecordInfo,
  getSupplyInfo,
  exportPurchaseDataByView
}
